package com.gz.im.backend.exception;

import com.corundumstudio.socketio.SocketIOClient;
import com.corundumstudio.socketio.listener.ExceptionListenerAdapter;
import io.netty.channel.ChannelHandlerContext;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
import java.util.List;

/**
 * @author GuoZhong
 * @description 消息异常
 * @date 2023/3/27 23:50
 */
@Slf4j
public class InstantMessageExceptionListener extends ExceptionListenerAdapter {
	@Override
	public void onEventException(Exception e, List<Object> args, SocketIOClient client) {
		log.error("onEventException is error",e);
		client.disconnect();
	}

	@Override
	public void onDisconnectException(Exception e, SocketIOClient client) {
		if(e instanceof IOException){
			log.info(e.getMessage());
		}else{
			log.error(e.getMessage(), e);
		}
		client.disconnect();
	}

	@Override
	public void onConnectException(Exception e, SocketIOClient client) {
		if(e instanceof IOException){
			log.info(e.getMessage());
		}else{
			log.error(e.getMessage(), e);
		}
		client.disconnect();
	}

	@Override
	public boolean exceptionCaught(ChannelHandlerContext ctx, Throwable e) throws Exception {
		if(e instanceof IOException){
			log.info(e.getMessage());
		}else{
			log.error(e.getMessage(), e);
		}
		ctx.close();
		return true;
	}
}
